/**
 * This class represents the collection of telemetry streams that have been added to the chart.
 */
package org.hackystat.ui.telemetryviewer.client;

import org.hackystat.ui.telemetryviewer.client.common.DatasetParcel;
import org.hackystat.ui.telemetryviewer.client.common.DatasetConveyor;
import org.timepedia.chronoscope.client.XYDataset;
import org.timepedia.chronoscope.client.data.ArrayXYDataset;

/**
 * This class represents the collection of telemetry streams that have been added to the chart.
 * @author David J. Nickles
 *
 */
public class XYDatasetFactory {
  
  private static XYDatasetFactory singular;
  private String key;
  private XYDataset[] cDataset;

  /**
   * Constructor.
   */
  private XYDatasetFactory() {
    this.key = null;
    this.cDataset = new ArrayXYDataset[0];
  }
  
  /**
   * Get a reference to the single XYDatasetFactory.
   * @return a singular reference to a XYDatasetFactory that this class maintains.
   */
  public static XYDatasetFactory getInstance() {
    if (singular == null) {
      return (singular = new XYDatasetFactory());
    } 
    else {
      return singular;
    }
  }
  
  /**
   * Adds the data set under the given key.
   * @param key name of the data stream.
   * @param datasetCollection the data structure for the data streams.
   */
  public void addDataset(String key, DatasetConveyor datasetCollection) {
    this.cDataset = new ArrayXYDataset[datasetCollection.getStreams().size()];
    this.key = key;
    for (int i = 0; i < datasetCollection.getStreams().size(); i++) {
      DatasetParcel dataset = (DatasetParcel)datasetCollection.getStreams().get(i);
      XYDataset xydataset = new ArrayXYDataset(dataset.getIdentifier(),
        dataset.getDomain(), dataset.getRange(), dataset.getLabel(), dataset.getAxisId());
      this.cDataset[i] = xydataset;
    }
  }
  
  /**
   * Gets the array of data sets for the chart.
   * @return an array of XYDatasets for the chart.
   */
  public XYDataset[] getDataset() {
    return this.cDataset;
  }
}
